package com.uzai.console.web.alarm;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.alarm.alarmdevice.AlarmDeviceQuery;
import com.uzai.console.dto.alarm.alarmrule.AlarmRuleQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatAlarmRuleIdDto;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatAlarmRuleIdListDto;
import com.uzai.console.entity.AlarmRule;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.mapper.AlarmRuleMapper;
import com.uzai.console.service.alarm.AlarmDeviceService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceWechatInfoVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备监控
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "设备监控",tags = "云监控|设备监控")
@ApiSort(19)
@RestController
public class AlarmDeviceController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(AlarmDeviceController.class);

    @Autowired
    private AlarmDeviceService alarmDeviceService;
    @Autowired
    private AlarmRuleMapper alarmRuleMapper;

    @ApiOperation(value = "分页查询设备监控列表", notes = "分页查询设备监控列表", response = DeviceWechatInfoVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/alarmDevice/findAlarmDeviceInfoList")
    public Object findAlarmDeviceInfoList(@RequestBody AlarmDeviceQuery alarmDeviceQuery){
        return alarmDeviceService.findAlarmDeviceInfoList(alarmDeviceQuery);
    }

    @SystemLogType(model = SystemModelEnum.AlarmDevice, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "配置报警规则", notes = "配置报警规则" )
    @ApiOperationSupport(order = 2)
    @PostMapping("/alarmDevice/setAlarmDeviceAlarmRule")
    public Object setAlarmDeviceAlarmRule(@RequestBody DeviceWechatAlarmRuleIdDto deviceWechatAlarmRuleIdDto){
        alarmDeviceService.setAlarmDeviceAlarmRule(deviceWechatAlarmRuleIdDto);
        return true;
    }

    @ApiOperation(value = "查询报警规则下拉列表", notes = "查询报警规则下拉列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/alarmDevice/findAlarmRuleSelectList")
    public Object findAlarmRuleSelectList(@RequestBody AlarmRuleQuery alarmRuleQuery){
        List<SelectKeyVo> selectIdVoList = new ArrayList<>();
        alarmRuleQuery.setMerId(getLoginInfo().getMerId());
        List<AlarmRule> alarmRuleList = alarmRuleMapper.findByList(alarmRuleQuery);
        if(alarmRuleList != null){
            for (AlarmRule alarmRule : alarmRuleList){
                SelectKeyVo selectKeyVo = new SelectKeyVo();
                selectKeyVo.setKey(String.valueOf(alarmRule.getId()));
                selectKeyVo.setValue(alarmRule.getName());
                selectIdVoList.add(selectKeyVo);
            }
        }
        return selectIdVoList;
    }

    @SystemLogType(model = SystemModelEnum.AlarmDevice, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "批量配置报警规则", notes = "批量配置报警规则" )
    @ApiOperationSupport(order = 4)
    @PostMapping("/alarmDevice/batchSetAlarmDeviceAlarmRule")
    public Object batchSetAlarmDeviceAlarmRule(@RequestBody DeviceWechatAlarmRuleIdListDto deviceWechatAlarmRuleIdListDto){
        alarmDeviceService.batchSetAlarmDeviceAlarmRule(deviceWechatAlarmRuleIdListDto);
        return true;
    }

}
